﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

namespace LabMS.Web.BaseData
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetCourseMajors : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (string.IsNullOrEmpty(context.Request.QueryString["ID"].ToString()))
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write("{error: true}");
                }

                string courseID = context.Request.QueryString["ID"];

                LabMS.Model.CourseJson json = new LabMS.Model.CourseJson();
                json.ID = int.Parse(courseID);

                LabMS.BLL.ProCourse BProCourse = new LabMS.BLL.ProCourse();
                ExtendBLL.Class eClass = new ExtendBLL.Class();

                List<LabMS.Model.ProCourse> listPC = new List<LabMS.Model.ProCourse>();
                listPC = BProCourse.GetModelList("Cou_ID=" + courseID);

                foreach (LabMS.Model.ProCourse proCourse in listPC)
                {
                    LabMS.Model.MajorJson major = new LabMS.Model.MajorJson();
                    major.ID = proCourse.ProID.Value;
                    major.Name = eClass.ProessionalNameByID(major.ID);

                    json.Majors.Add(major);
                }

                string strJson = Newtonsoft.Json.JavaScriptConvert.SerializeObject(json);//序列化，把JSON转化为string类型

                context.Response.ContentType = "application/json";
                context.Response.Write(strJson);

                /*
                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                sb.Append("{Majors: [");
                //sb.Append("{");
                //sb.Append("ID: ");    
                //JSON格式为：{Majors:[{ID:1231,Name:'sfsds'},{ID:1111,Name:'lkjo'}]}
                for (int i = 0; i < listPC.Count; i++)
                {
                    int Pid = int.Parse(listPC[i].ProID.Value.ToString());
                    string PName = eClass.ProessionalNameByID(Pid);

                    sb.Append("{");
                    sb.Append("ID: ");
                    sb.Append(Pid);
                    sb.Append(",");
                    sb.Append("Name: ");
                    sb.Append(PName);
                    if (i != listPC.Count - 1)
                    {
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append("}");
                        sb.Append("]");
                        sb.Append("}");
                    }
                }

                //sb.Append("'1231'");
                //sb.Append(",");
                //sb.Append("Name: ");
                //sb.Append("'sfsds'");
                //sb.Append("}");

                //sb.Append(",");

                //sb.Append("{");
                //sb.Append("ID: ");
                //sb.Append("'1111'");
                //sb.Append(",");
                //sb.Append("Name: ");
                //sb.Append("'lkjo'");
                //sb.Append("}");

                //sb.Append("]}");
            
                context.Response.ContentType = "application/json";
                context.Response.Write(sb.ToString()); //返回值*/
            }
            catch
            {
                context.Response.ContentType = "application/json";
                context.Response.Write("{error: true}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
